/**
 * Date:2017年12月25日上午11:09:32
 *
 */

package com.xue.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.github.pagehelper.PageInfo;
import com.xue.AppContext;
import com.xue.common.core.AppException;
import com.xue.common.vo.ParamMap;
import com.xue.pojo.Demo;
import com.xue.service.IDemoService;

/**
 * Description: demo <br/>
 * Date: 2017年12月25日 上午11:09:32 <br/>
 * 
 * @author xuejianxinokok@163.com
 */

@RequestMapping(value = DemoController.PATH, name = "demo样例")
@RestController("demoController")
public class DemoController {
	public static final String PATH = "demo";

	@Autowired
	private RequestMappingHandlerMapping requestMappingHandlerMapping;

	@Autowired
	private IDemoService service;

	// 分页查询
	@PostMapping(value = "/listPage", name = "分页查询列表")
	public PageInfo<Demo> listPage(@RequestBody ParamMap pm) {
		return service.queryByPage(pm);
	}

	// 按ID查询
	@PostMapping(value = "/getById", name = "按id查询一条数据")
	public Demo getById(@RequestBody ParamMap pm) {
		return service.queryById(pm.getI("id"));
	}

	// 添加
	@PostMapping(value = "/add", name = "添加")
	public Integer add(@RequestBody Demo vo) {
		service.save(vo);
		return vo.getId();
	}

	// 更新
	@PostMapping(value = "/update", name = "更新")
	public Integer update(@RequestBody Demo vo) {
		return service.update(vo);

	}

	// 删除
	@PostMapping(value = "/deleteByIds", name = "删除")
	public Integer deleteByIds(@RequestBody List<Integer> ids) {
		return service.deleteByIds(ids);
	}

	/**
	 * 如果有业务异常应该抛出 AppException
	 */
	@GetMapping(value = "/error", name = "测试错误")
	public String error() {
		try {
			// System.out.println(1 / 0);
			service.saveTest(new Demo());
		} catch (Exception e) {
			throw new AppException("业务错误处理测试!" + e.getMessage());
		}
		return "ok";
	}

	@GetMapping(value = "/test1/{id}", name = "测试rest path")
	public String test1(@PathVariable("id") String id) {
		HttpServletRequest r = AppContext.getRequest();
		
		System.out.println(r.getRequestURI());//"/demo/test1/%E4%BD%A0%E5%A5%BD"
		System.out.println(r.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));//"/demo/test1/{id}"
		System.out.println(r.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));//"com.xue.action.DemoController#test1(String)"
		System.out.println(r.getAttribute(HandlerMapping.LOOKUP_PATH));///demo/test1/你好
		System.out.println(r.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));//"/demo/test1/你好"
		System.out.println(r.getAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING));//"null"
		System.out.println(r.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE));//"{id=你好}"
		System.out.println(r.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE));//"{id=你好}"
		return id;
	}

	// @RequestMapping(value = {"/mappings","/mp"}, name = "获取页面所有url和执行的方法")
	@RequestMapping(value = "/mappings", name = "获取页面所有url和执行的方法")
	public List<HashMap<String, String>> list() {
		List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();

		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
		for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
			HashMap<String, String> en = new HashMap<String, String>();

			RequestMappingInfo info = m.getKey();
			HandlerMethod hm = m.getValue();

			PatternsRequestCondition p = info.getPatternsCondition();
			// 一个方法可能对应多个url
			// @RequestMapping(value = {"/mappings","/mp"}, name = "获取页面所有url和执行的方法")
			en.put("url", p.getPatterns().stream().collect(Collectors.joining(";")));
			en.put("className", hm.getBeanType().getName()); // 类名
			en.put("method", hm.getMethod().getName()); // 方法名
			en.put("remark", info.getName());// 备注信息
			RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
			String type = methodsCondition.toString();
			if (type != null && type.startsWith("[") && type.endsWith("]")) {
				type = type.substring(1, type.length() - 1);
				en.put("type", type); // 方法名
			}
			urlList.add(en);
		}
		return urlList;
	}

}
